﻿using Dapper;
using System.Data.SqlClient;
using yunbo.Model.IM;

namespace yunbo.Repositorys.IMRepositorys
{
    public class WMS_CKPDXXRepository
    {
        public string data = "Data Source=121.37.93.71;Initial Catalog=FanXingMES;User ID=sa;Password=geili@2025;";
        public int PostWMS_CKPDXX(TJWMS_CKPDXX wMS_CKPDXX)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"insert into [dbo].[wms_ckpdxx]([ckpdxx_code],[ckpd_code],[ck_code],[kq_code],[kw_code],[wl_code],[ckpdxx_wlmc],[ckpdxx_wllx],[ckpdxx_wldw],[ckpdxx_ggxh],[ckpdxx_zmkcsl],[ckpdxx_wldj],[ckpdxx_state])\r\n" +
                    $"values('{wMS_CKPDXX.ckpdxx_code}','{wMS_CKPDXX.ckpd_code}','{wMS_CKPDXX.ck_code}','{wMS_CKPDXX.kq_code}','{wMS_CKPDXX.kw_code}','{wMS_CKPDXX.wl_code}','{wMS_CKPDXX.ckpdxx_wlmc}','{wMS_CKPDXX.ckpdxx_wllx}','{wMS_CKPDXX.ckpdxx_wldw}','{wMS_CKPDXX.ckpdxx_ggxh}',{wMS_CKPDXX.ckpdxx_zmkcsl},{wMS_CKPDXX.ckpdxx_dj},1)";
                int rs = connection.Execute(sql);
                return rs;
            }
        }
        public FYCXWMS_CKPDXX GetCXWMS_CKPDXX(int PageNumber, int PageSize,string code)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"select a.pdtz_code,a.ck_code,a.kq_code,a.kw_code,a.ckpdxx_state,a.ckpdxx_id,a.ckpdxx_code,ckpd_code,b.ck_name,c.kq_name,d.kw_name,a.wl_code,a.ckpdxx_wlmc,a.ckpdxx_wllx,ckpdxx_wldw,a.ckpdxx_ggxh,a.ckpdxx_zmkcsl,a.ckpdxx_pdzsl,a.ckpdxx_wldj,a.ckpdxx_ykzsl from [dbo].[wms_ckpdxx] a\r\n" +
                    $"inner join [dbo].[wbd_ck] b on a.ck_code=b.ck_code\r\n" +
                    $"inner join [dbo].[wbd_kq] c on a.kq_code=c.kq_code\r\n" +
                    $"inner join [dbo].[wbd_kw] d on a.kw_code=d.kw_code\r\n" +
                    $"where ckpd_code='{code}'\r\n" +
                    $"ORDER BY  ckpdxx_id \r\n" +
                    $"OFFSET ({PageNumber}-1)*{PageSize} ROWS\r\n" +
                    $"FETCH NEXT {PageSize} ROWS ONLY;";
                List<CXWMS_CKPDXX> cXWMS_CKPDXXs = connection.Query<CXWMS_CKPDXX>(sql).ToList();
                string sql1 = $"select COUNT(*) from [dbo].[wms_ckpdxx] a\r\ninner join [dbo].[wbd_ck] b on a.ck_code=b.ck_code\r\ninner join [dbo].[wbd_kq] c on a.kq_code=c.kq_code\r\ninner join [dbo].[wbd_kw] d on a.kw_code=d.kw_code\r\nwhere ckpd_code='{code}'";
                int zon = connection.Query<int>(sql1).FirstOrDefault();
                FYCXWMS_CKPDXX fYCXWMS = new FYCXWMS_CKPDXX()
                {
                    cXWMS_CKPDXXs = cXWMS_CKPDXXs,
                    zon = zon,
                };
                return fYCXWMS;
            }
        }
        public int UpdateWMS_CKPDXX(int pdsl, decimal sl, string code)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"update wms_ckpdxx set [ckpdxx_pdzsl]={pdsl},[ckpdxx_ykzsl]={sl},[ckpdxx_state]=2\r\nwhere [ckpdxx_code]='{code}'";
                int rs = connection.Execute(sql);
                return rs;
            }
        }
        public int UpdateWMS_CKPDXXZT(string code,string pdtzcode,string state)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"update wms_ckpdxx set [ckpdxx_state]={state},pdtz_code='{pdtzcode}'\r\nwhere [ckpdxx_code]='{code}'";
                int rs = connection.Execute(sql);
                return rs;
            }
        }
    }
}
